/*
 * DimenTools.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * dimens 文件生成工具
 *
 * <p>Created by ChenTao(chentao7v@gmail.com) on 2017/2/8 15:26
 */
public class DimenTools {

    private static final float DEFAULT_WIDTH = 360f;

    public static void genDimens() {

        File file = new File("./app/src/main/res/values/dimens.xml");
        BufferedReader reader = null;

        StringBuilder w240 = new StringBuilder();
        StringBuilder w320 = new StringBuilder();
        StringBuilder w360 = new StringBuilder();
        StringBuilder w420 = new StringBuilder();
        StringBuilder w560 = new StringBuilder();

        try {
            reader = new BufferedReader(new FileReader(file));
            String line;
            int lineCount = 1;
            while ((line = reader.readLine()) != null) {

                if (line.contains("</dimen>")) {
                    String start = line.substring(0, line.indexOf(">") + 1);
                    String end = line.substring(line.lastIndexOf("<") - 2);
                    float num =
                            Float.valueOf(
                                    line.substring(
                                            line.indexOf(">") + 1, line.indexOf("</dimen>") - 2));

                    w240.append(start)
                            .append((int) (num * 240 / DEFAULT_WIDTH * 100) / 100f)
                            .append(end)
                            .append("\n");
                    w320.append(start)
                            .append((int) (num * 320 / DEFAULT_WIDTH * 100) / 100f)
                            .append(end)
                            .append("\n");
                    w360.append(start)
                            .append((int) (num * 360 / DEFAULT_WIDTH * 100) / 100f)
                            .append(end)
                            .append("\n");
                    w420.append(start)
                            .append((int) (num * 420 / DEFAULT_WIDTH * 100) / 100f)
                            .append(end)
                            .append("\n");
                    w560.append(start)
                            .append((int) (num * 560 / DEFAULT_WIDTH * 100) / 100f)
                            .append(end)
                            .append("\n");

                } else {
                    w240.append(line).append("\n");
                    w320.append(line).append("\n");
                    w360.append(line).append("\n");
                    w420.append(line).append("\n");
                    w560.append(line).append("\n");
                }
                lineCount++;
            }
            reader.close();

            String w240file = "./app/src/main/res/values-w240dp/dimens.xml";
            String w320file = "./app/src/main/res/values-w320dp/dimens.xml";
            String w360file = "./app/src/main/res/values-w360dp/dimens.xml";
            String w420file = "./app/src/main/res/values-w420dp/dimens.xml";
            String w560file = "./app/src/main/res/values-w560dp/dimens.xml";

            writeFile(w240file, w240.toString());
            writeFile(w320file, w320.toString());
            writeFile(w360file, w360.toString());
            writeFile(w420file, w420.toString());
            writeFile(w560file, w560.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    public static void writeFile(String path, String text) {

        File file = new File(path);
        String parent = file.getParent();

        File parDir = new File(parent);

        if (!parDir.exists()) {
            parDir.mkdirs();
        }

        PrintWriter out = null;
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            out = new PrintWriter(new BufferedWriter(new FileWriter(path)));
            out.println(text);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    public static void main(String[] args) {
        genDimens();
    }
}
